package com.xxx.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.Client;
import com.xxx.pojo.ClientDetail;
import com.xxx.pojo.Label;
import com.xxx.pojo.deleted.ClientDeleted;
import com.xxx.pojo.vo.ClientVo;
import com.xxx.service.ClientDeletedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-04
 */
@Api(tags = "客户回收")
@CrossOrigin
@RestController
@RequestMapping("/clientDeleted")
public class ClientDeletedController {

    @Resource
    private ClientDeletedService clientDeletedService;

    /**
     * 查询所有已被禁用的客户
     *
     * @return 返回操作结果
     */
    @ApiOperation(value = "查询被禁用的客户列表", notes = "查询所有被禁用的客户")
    @PostMapping("/disabledClients")
    public Request getDisabledClients(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<ClientVo> mapPage = new Page<>(pageNum, pageSize, true);

        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

//        左连表
        MPJLambdaWrapper<ClientDeleted> lambdaWrapper = new MPJLambdaWrapper<ClientDeleted>()
                .select(ClientDeleted::getClientWechat,ClientDeleted::getClientName,
                        ClientDeleted::getClientId,ClientDeleted::getClientVip,
                        ClientDeleted::getDeleted,ClientDeleted::getClientSuperior,
                        ClientDeleted::getGmtCreation,ClientDeleted::getClientBalance)
                .select(ClientDetail::getClientDetailAddress)
                .selectAll(Label.class)
                .leftJoin(ClientDetail.class, ClientDetail::getClientId, Client::getClientId)
                .leftJoin(Label.class, Label::getLabelId, ClientDetail::getLabelId);

//        条件查询
        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
                    .like(Client::getClientName, finalSearchValue)
                    .or()
                    .like(Client::getClientEmail, finalSearchValue)
                    .or()
                    .like(Client::getClientWechat, finalSearchValue));
        }

        clientDeletedService.selectJoinListPage(mapPage, ClientVo.class, lambdaWrapper);

        List<ClientVo> records = mapPage.getRecords();
        long total = mapPage.getTotal();

        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询失败");
    }


    /**
     * 批量恢复用户权限
     *
     * @param clientId 客户编号
     * @return 返回操作结果
     */
    @ApiOperation(value = "批量恢复用户权限", notes = "根据工号批量恢复用户权限")
    @DeleteMapping("/restorePermissionsBatch")
    public Request restorePermissionsBatch(@RequestParam("clientId") List<Long> clientId) {
        boolean restored = clientDeletedService.removeByIds(clientId);
        return restored ? Request.suc() : Request.error("根据工号批量恢复用户权限失败");
    }


}

